package net.wangds.auth.job.comp;

import net.wangds.auth.job.entity.GeneralJobEntity;
import net.wangds.auth.job.entity.GeneralJobRoleRef;
import net.wangds.auth.job.entity.GeneralJobUserRef;
import net.wangds.auth.job.repo.rdbms.JobJpaRepo;
import net.wangds.auth.job.repo.rdbms.JobResRefJpaRepo;
import net.wangds.auth.job.repo.rdbms.JobRoleRefRepo;
import net.wangds.auth.job.repo.rdbms.JobUserRefRepo;
import net.wangds.auth.module.Job;
import net.wangds.auth.module.Resource;
import net.wangds.auth.module.Role;
import net.wangds.auth.module.User;
import net.wangds.data.RemovedEnum;
import net.wangds.jdbc.comp.jdbc.JdbcComp;
import net.wangds.jpa.Sorts;
import net.wangds.jpa.comp.AbstractRemovedComp;
import net.wangds.log.helper.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

@Component
public class GeneralJobComp extends AbstractRemovedComp<GeneralJobEntity> {

    @Autowired
    private JobRoleRefRepo mmJpaJobRole;

    @Autowired
    private JobUserRefRepo mmJpaJobUser;

    @Autowired
    private JdbcComp mmJdbc;

    /**
     * 构造函数.
     *
     * @param repo 数据库操作对象.
     */
    protected GeneralJobComp(JobJpaRepo repo) {
        super(repo);
    }


    public void bindRole(Job job, Role role) {

        String jobId = job.getId();
        String roleId = role.getId();
        GeneralJobRoleRef ref = mmJpaJobRole.findByJobIdAndRoleId(jobId, roleId)
                .orElseGet(() -> GeneralJobRoleRef.builder().jobId(jobId).roleId(roleId).build());

        ref.setRemoved(RemovedEnum.NORMAL);
        mmJpaJobRole.saveAndFlush(ref);
    }


    public void unbindRole(Job job, Role role) {

        String jobId = job.getId();
        String roleId = role.getId();
        mmJpaJobRole.findByJobIdAndRoleId(jobId, roleId).ifPresent(ref -> {
            mmJpaJobRole.delete(ref);
            mmJpaJobRole.flush();
        });

    }

    public void bindUser(Job job, User user) {
        String jobId = job.getId();
        String uid = user.getId();
        GeneralJobUserRef ref = mmJpaJobUser.findByJobIdAndUserId(jobId, uid)
                .orElseGet(() -> GeneralJobUserRef.builder().build());
        ref.setRemoved(RemovedEnum.NORMAL);
        mmJpaJobUser.saveAndFlush(ref);
    }

    public void unbindUser(Job job, User user) {
        String jobId = job.getId();
        String uid = user.getId();
        mmJpaJobUser.findByJobIdAndUserId(jobId, uid).ifPresent(ref ->
                mmJpaJobUser.delete(ref)
        );
    }

    public List<GeneralJobRoleRef> loadRoles(String jobId) {
        return mmJpaJobRole.findByJobIdAndRemoved(jobId, RemovedEnum.NORMAL, Sorts.createTm_desc);
    }

    @Override
    public Page<GeneralJobEntity> findByKeywords(String keywords, Pageable paging) {
        String sql = "SELECT t1.* FROM cnpgmt.auth_job t1\n" +
                "where ( t1.job_name regexp ? or t1.job_desc regexp ? ) and t1.removed = 0";

        return mmJdbc.executeQuery(sql, new Object[]{keywords, keywords}, paging, GeneralJobEntity.class);
    }
}
